package com.xique.park.service.service.impl;

import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.hutool.core.collection.CollUtil;
import com.xique.park.bean.domain.ParkingOrder;
import com.xique.park.mapper.es.ParkingOrderEsMapper;
import com.xique.park.service.service.IParkingOrderEsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ParkingOrderEsServiceImpl
 *
 * @author WLD
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ParkingOrderEsServiceImpl implements IParkingOrderEsService {

    private final ParkingOrderEsMapper parkingOrderEsMapper;

    @Override
    public void saveOrUpdate(ParkingOrder parkingOrder) {
        long id = parkingOrder.getId();
        if (selectById(id) == null) {
            parkingOrderEsMapper.insert(parkingOrder);
            log.info("[车辆订单服务]-ES中新增订单记录信息：{}", parkingOrder);
        } else {
            parkingOrderEsMapper.updateById(parkingOrder);
            log.info("[车辆订单服务]-ES中更新订单记录信息：{}", parkingOrder);
        }
    }

    @Override
    public void batchSave(List<ParkingOrder> parkingOrderList) {
        if (CollUtil.isNotEmpty(parkingOrderList)) {
            parkingOrderEsMapper.insertBatch(parkingOrderList);
            log.info("[车辆订单服务]-ES中批量新增订单记录信息：批量新增数目:{}", parkingOrderList.size());
        }
    }

    @Override
    public PageInfo<ParkingOrder> pageQuery(LambdaEsQueryWrapper<ParkingOrder> queryWrapper, Integer pageNum, Integer pageSize) {
        return parkingOrderEsMapper.pageQuery(queryWrapper, pageNum, pageSize);
    }

    @Override
    public SearchResponse search(LambdaEsQueryWrapper<ParkingOrder> queryWrapper) {
        return parkingOrderEsMapper.search(queryWrapper);
    }

    @Override
    public ParkingOrder selectById(Long id) {
        ParkingOrder parkingOrder = parkingOrderEsMapper.selectById(id);
        log.info("[车辆订单服务]-根据ID在ES中查询订单信息：{},{}", id, parkingOrder);
        return parkingOrder;
    }

}
